package com.ws.shop.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.AuctionDao;
import com.ws.shop.entity.Auction;
import com.ws.shop.entity.AuctionLog;

import com.ws.shop.service.AuctionService;
@Service(value="auctionService")
public class AuctionServiceImpl implements AuctionService {
	@Resource(name="auctionDao")
	private AuctionDao auctionDao;
	
	@Cacheable(value="auctionColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Map<String,Object>> findeAuctionList(final int state,final int limit){
		return auctionDao.findeAuctionList(state, limit);
	}
	
	/**
	 *查询竞拍日志 
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public AuctionLog findAuctionLog(final long aid,final int uid){
		return auctionDao.findAuctionLog(aid, uid);
	}
	
	public double beatsum(final long aid,final int uid){
		return  auctionDao.beatsum(aid,uid);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public boolean issaveprice(final int uid,final long aid){
		return auctionDao.issaveprice(uid,aid);
	}
	
	@Cacheable(value="auction",key="'findById_'+#id")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Auction findAuctionById(int id) {
		return auctionDao.findAuctionById(id);
	}

	@Cacheable(value="auctionColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page findAuctionListPage(int pageno, int pagesize, String goodsname) {
		return auctionDao.findAuctionListPage(pageno, pagesize, goodsname);
	}

	@Transactional(rollbackFor=Exception.class)
	public void saveEnd(Auction auction) {
		// TODO Auto-generated method stub
		
	}

	@Caching(evict={@CacheEvict(value="auction",key="'findById_'+#auction.id",condition="#auction.id > 0"),@CacheEvict(value="auctionColl",allEntries=true)})
	@Transactional(rollbackFor=Exception.class)
	public void saveOrUpdate(Auction auction) {
		auctionDao.saveOrUpdate(auction);
	}

	@Transactional(rollbackFor=Exception.class)
	public int saveInsured(int uid, long aid) {
		return auctionDao.saveInsured(uid, aid);
	}
	
	@Cacheable(value="auctionColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Map<String,Object>> getHead() throws SQLException{
		return auctionDao.getHead();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public String getids(int state) throws SQLException{
		return auctionDao.getids(state);
	}

	@Cacheable(value="auctionColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Map<String, Object>> getRecommend(String date)
			throws SQLException {
		return auctionDao.getRecommend(date);
	}
	
	//更新开始竞拍状态
	@Caching(evict={@CacheEvict(value="auction",key="'findById_'+#id"),@CacheEvict(value="auctionColl",allEntries=true)})
	@Transactional(rollbackFor=Exception.class)
	public int updateState(int id,int state){
		return auctionDao.updateState(id,state);
	}
	
	@Cacheable(value="auctionColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Auction> showAuction(String condition ){
		return auctionDao.showAuction(condition);
	}
 

	@Cacheable(value="auctionColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page findAuctionListPage(int pageno, int pagesize, int cid,int state,int isfree) {
		return auctionDao.findAuctionListPage(pageno,pagesize,cid,state,isfree);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Map<String, Object>> findAuctionNeedStart() {
		return auctionDao.findAuctionNeedStart();
	}

	public Auction bids(int uid, int aid) {
		return auctionDao.bids(uid, aid);
	}

	public Page findSelectRobotsByAidPage(int pageno, int pagesize, int aid) {
		return auctionDao.findSelectRobotsByAidPage(pageno,pagesize,aid);
	}
	
	public void userAuctioningList(final Page page,final int uid){
		auctionDao.userAuctioningList(page, uid);
	}

	@Transactional(rollbackFor=Exception.class)
	public void addauctionrobot(int aid, int uid) {
		auctionDao.addauctionrobot(aid,uid);
	}

	@Transactional(rollbackFor=Exception.class)
	public void delauctionrobot(int rid) {
		auctionDao.delauctionrobot(rid);
	}
	
	//成交商品数量
	public int getAuctionGoodsTradingVolume(int state){
		return auctionDao.getAuctionGoodsTradingVolume(state);
	}

	@Cacheable(value="auctionColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Map<String, Object>> findAuctionList(int startrow,
			int limit,int state) {
		return auctionDao.findAuctionList(startrow,limit,state);
	}
	
	@Caching(evict={@CacheEvict(value="auction",key="'findById_'+#id"),@CacheEvict(value="auctionColl",allEntries=true)})
	@Transactional(rollbackFor=Exception.class)
	public int updateAuctionState(final Auction auction){
		return auctionDao.updateAuctionState(auction);
	}

	@Caching(evict={@CacheEvict(value="auction",key="'findById_'+#id"),@CacheEvict(value="auctionColl",allEntries=true)})
	@Transactional(rollbackFor=Exception.class)
	public void updateHtml(long id, String htmlFilePath) {
		auctionDao.updateHtml(id,htmlFilePath);
	}
}

